package com.fsoft.core.utils.excel;

import java.util.regex.Pattern;

import org.jxls.expression.ExpressionEvaluator;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.TransformationConfig;

/***
 * @package com.fsoft.core.utils.excel
 * @author fish
 * @email it.fish2010@foxmail.com
 * @date 2021-09-30
 * @desc Jxls 自定义转换配置类 <BR>
 *       默认表达式语法：${xx.xxx} <BR>
 *       当前表达式语法：[:xx.xxx]
 *
 */
public class CustomTransformationConfig extends TransformationConfig {

	private static final String EXPRESSION_PART = "(.+?)";
	private static final String DEFAULT_EXPRESSION_BEGIN = "[:";
	private static final String DEFAULT_EXPRESSION_END = "]";
	private static final String DEFAULT_REGEX_EXPRESSION = "\\[:(.*?)]";

	private ExpressionEvaluator expressionEvaluator = new JexlExpressionEvaluator();
	private String expressionNotationBegin = DEFAULT_EXPRESSION_BEGIN;
	private String expressionNotationEnd = DEFAULT_EXPRESSION_END;
	private Pattern expressionNotationPattern = Pattern.compile(DEFAULT_REGEX_EXPRESSION);

	@Override
	public void buildExpressionNotation(String expressionBegin, String expressionEnd) {
		this.expressionNotationBegin = expressionBegin;
		this.expressionNotationEnd = expressionEnd;
		String regexExpression = Pattern.quote(expressionNotationBegin) + EXPRESSION_PART + Pattern.quote(expressionNotationEnd);
		expressionNotationPattern = Pattern.compile(regexExpression);
	}

	@Override
	public ExpressionEvaluator getExpressionEvaluator() {
		return expressionEvaluator;
	}

	@Override
	public void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator) {
		this.expressionEvaluator = expressionEvaluator;
	}

	@Override
	public String getExpressionNotationBegin() {
		return expressionNotationBegin;
	}

	@Override
	public String getExpressionNotationEnd() {
		return expressionNotationEnd;
	}

	@Override
	public Pattern getExpressionNotationPattern() {
		return expressionNotationPattern;
	}

}
